package de.seminar.lego3D.views;

import de.seminar.lego3D.Constants;
import de.seminar.lego3D.Legobrick;
import de.seminar.lego3D.R;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class LegoView extends LinearLayout {

	/** gridview containing the lego bricks **/
	private GridView brickView;
	
	/** array containing an object for each lego brick **/
	private Legobrick[] lego_bricks;
	
	/** context of this view **/
	private Context ctx;
	
	/** the current lego brick and the current color **/
	private ImageView currentBrick, currentColor;
	
	/** handler to send callbacks to the main activity, if another brick was chosen **/
	private Handler mHandler;
	
	/** reference to the send button **/
	private Button sendBtn;

	/**
	 * Creates a new lego view.
	 * @param context of the main activity
	 * @param handler - to send back the information of a chosen brick
	 */
	public LegoView(Context context, Handler handler) {
		super(context);

		ctx = context;
		mHandler = handler;
		
		final View content = LayoutInflater.from(context).inflate(
				R.layout.lego_screen, null, false);

		brickView = (GridView) content.findViewById(R.id.lego_bricks);
		//currentColor = (ImageView) content.findViewById(R.id.current_color);
		currentBrick = (ImageView) content.findViewById(R.id.current_lego);
		sendBtn = (Button) content.findViewById(R.id.sendButton);

		brickView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				
//				Toast.makeText(ctx,
//						"clicked on lego brick number " + position
//								+ ". It has width: "
//								+ lego_bricks[position].width
//								+ ", and height: "
//								+ lego_bricks[position].height + ".",
//						Toast.LENGTH_SHORT).show();
//
				currentBrick.setImageDrawable(lego_bricks[position].getDrawableIcon());
				currentBrick.setBackgroundColor(lego_bricks[position].color);
				
				final Bundle bundle = new Bundle();
				bundle.putIntArray(Constants.LEGO_LAYOUT, lego_bricks[position].getLayout());
				final Message msg = new Message();
				msg.setData(bundle);
				mHandler.sendMessage(msg);
			}
		});

		initBricks();

		this.addView(content);
	}
	
	/**
	 * Returns a reference to the send button.
	 * @return
	 */
	public Button getSendButton() {
		return sendBtn;
	}

	/**
	 * Initializes the array and fills the view.
	 */
	private void initBricks() {
		lego_bricks = new Legobrick[8];
		lego_bricks[0] = new Legobrick(R.drawable.lego1, ctx, 1);
		lego_bricks[1] = new Legobrick(R.drawable.lego2, ctx, 1, 1);
		lego_bricks[2] = new Legobrick(R.drawable.lego3, ctx, 1, 1, 1);
		lego_bricks[3] = new Legobrick(R.drawable.lego4, ctx, 1, 1, 1, 1);
		lego_bricks[4] = new Legobrick(R.drawable.lego5, ctx, 2, 2, 2, 2);
		lego_bricks[5] = new Legobrick(R.drawable.lego6, ctx, 2, 2);
		lego_bricks[6] = new Legobrick(R.drawable.lego7, ctx, 2, 2, 2);
		lego_bricks[7] = new Legobrick(R.drawable.lego8, ctx, 1, 2);

		brickView.setAdapter(new ImageAdapter(ctx));
	}

//	@Override
//	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//
//		int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
//		int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
//		this.setMeasuredDimension(parentWidth / 2, parentHeight);
//	}
	
	/**
	 * Adapter for the gridview containing the images of the lego bricks
	 * @author christian
	 *
	 */
	public class ImageAdapter extends BaseAdapter {

		/**
		 * Creates a new adapter
		 * @param c
		 */
	    public ImageAdapter(Context c) {
	    }

	    /**
	     * Returns the number of items of the gridview
	     */
	    public int getCount() {
	        return lego_bricks.length;
	    }

	    /**
	     * Returns a object reference to an item at the position.
	     */
	    public Object getItem(int position) {
	        return lego_bricks[position];
	    }

	    /**
	     * Returns the item ID.
	     */
	    public long getItemId(int position) {
	        return position;
	    }

	    /**
	     *  create a new ImageView for each item referenced by the Adapter
	     */
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	View v = lego_bricks[position].getIcon();
	        return v;
	    }
	}

	/**
	 * Sets the current color view to the given color.
	 * @param color
	 */
	public void setCurrentColorView(int color) {
		//currentColor.setImageResource(R.drawable.color_drop);
		//currentColor.setBackgroundColor(color);
		
		if (currentBrick.getDrawable() != null) {
			currentBrick.setBackgroundColor(color);
		}
		
		for (int i = 0; i < lego_bricks.length; i++) {
			lego_bricks[i].setColor(color);
			lego_bricks[i].getIcon().setBackgroundColor(color);
		}
	}
	
	/**
	 * Clears the selection.
	 */
	public void clearSelection() {
		currentBrick.setImageDrawable(null);
		//currentColor.setImageDrawable(null);
		//currentColor.invalidate();
		currentBrick.invalidate();
	}
}
